export default {
  //自定义节流操作
  preventReClick: {
    mounted(el: any, binding: any) {
      el.addEventListener('click', () => {
        if (!el.disabled) {
          el.disabled = true
          el.style.backgroundColor = '#ccc'
          el.style.border = 'none'
          setTimeout(() => {
            el.disabled = false
            el.style.backgroundColor = '#1890ff'
            el.style.border = '1px solid #1890ff'
          }, binding.value || 1000) //2000ms间隔时间
        }
      })
    }
  },
  //自定义触底加载更多数据
  scroll: {
    mounted(el: any, binding: any) {
      let lastScrollTime = 0
      const scrollDelay = 1000
      window.addEventListener('scroll', () => {
        // document.documentElement.scrollTop //距离顶部的距离
        // document.documentElement.clientHeight//表示可视区域高度， 包括padding但不包括border、水平滚动条、margin的元素的高度
        // document.documentElement.scrollHeight// 表示页面的高度，包括溢出页面
        //document.documentElement表示文档的根元素，即html元素
        // document.body 表示文档中body元素
        // console.log(el.clientHeight);
        const scrollHeight = document.documentElement.scrollHeight
        const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
        const clientHeight = document.documentElement.clientHeight
        let loading = false
        // console.log(scrollHeight, scrollTop, clientHeight, el.clientHeight)

        const loadMore = binding.value
        const currentTime = new Date().getTime()
        if (
          currentTime - lastScrollTime > scrollDelay &&
          scrollTop + clientHeight >= scrollHeight &&
          !loading &&
          loadMore instanceof Function
        ) {
          loading = true
          console.log('scrool')
          loadMore(() => {
            loading = false
          })
          lastScrollTime = currentTime
        }
      })
    }
  }
}
